import json
import re
import sys

from config import getConfigParams
from requestUrl import getDocByUrl,getEntryIdByUrl
from formatContent import removeSrcParams, formatContent
from logs import saveLog

# 获取配置参数
configParams = getConfigParams(sys.argv)
url = configParams['real_url']
env = configParams['env']

# 获取文章ID
entryId = getEntryIdByUrl(configParams['base_url'])

headers = {
    'Host': 'www.jianshu.com',
}

# 获取html文本doc，得到pq对象
doc = getDocByUrl(url=url, headers=headers)

# 解析script
if doc('script[id="__NEXT_DATA__"]').html()[0] == '"':
    scriptContent =  doc('script[id="__NEXT_DATA__"]').html()[1:-1]
    script = json.dumps(scriptContent)
    scriptData = json.loads(eval("'{}'".format(script)))
else:
    scriptContent = doc('script[id="__NEXT_DATA__"]').html()
    scriptData = json.loads(scriptContent)

# 封面图
coverUrl = re.findall("<meta.*?property=\"og:image\" content=\"([^\"]+)\"/>", doc.html())[0]

# 通过meta标签获取的图片是作者头像的话，封面图置空
authorAvatar = scriptData['props']['initialState']['note']['data']['user']['avatar']
if coverUrl == authorAvatar:
    coverUrl = ''

# 标题
title = scriptData['props']['initialState']['note']['data']['public_title']

# 文章作者
authorName = scriptData['props']['initialState']['note']['data']['user']['nickname']

# 文章正文
# 并将图片？后的参数去掉，否则无图
articleContent = removeSrcParams(doc('article')).html()

# 发布时间
publicTime = scriptData['props']['initialState']['note']['data']['last_updated_at']

# 去【李世泉】那里过滤标签
content = formatContent(articleContent,env=env)

result = {
    'entry_id': entryId,
    'not_format_content': articleContent,
    'content': content,
    'author_name': authorName,
    'cover_url': coverUrl,
    'public_time': publicTime,
    'description': '',
    'title': title,
}
logData = {
    'params': sys.argv,
    'result': result
}
saveLog(json.dumps(logData, ensure_ascii=False))
# 结果输出
print(json.dumps(result, ensure_ascii=False))
